package gui;

import java.awt.Font;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

import client.Main;
import client.MemberApproval;

public class MemberApprovalGUI extends JFrame implements TableModelListener{
	
	MemberApproval map;
	
	private JPanel panel;
	
	// labels
	private JLabel lblHeader;
	
	// buttons
	public JButton btnBack;
	
	// table
	private JTable tblTable;
	
	private JScrollPane scrlTable;
	
	private JScrollPane getScrollTable()
	{
		if(scrlTable==null)
		{
			scrlTable=new JScrollPane(getTable());
			scrlTable.setBounds(20, 60, 450, 200);
			
		}
		return scrlTable;
	}
	
	private JTable getTable()
	{
		if(tblTable==null)
		{
			tblTable=new JTable(new MemberApproval());
			tblTable.getModel().addTableModelListener(this);
			tblTable.setFillsViewportHeight(true);
			tblTable.setCellEditor(null);
		}
		return tblTable;
	}
	
	public void tableChanged(TableModelEvent e) {
		TableModel model = (TableModel)e.getSource();
		
		int row = e.getFirstRow();
		int column=e.getColumn();
		Object data = model.getValueAt(row, column);
		boolean k=Boolean.parseBoolean(String.valueOf(data));

		if(column==2)
		{	
			if(k==true)
			{
				if(map.updateData(row,1)==1)
					JOptionPane.showMessageDialog(null, "Member Approved");
				else
					JOptionPane.showMessageDialog(null, "Could not approve");
			}
			else
			{
				if(map.updateData(row, 0)==1)
					JOptionPane.showMessageDialog(null, "Member disApproved");
				else
					JOptionPane.showMessageDialog(null, "Could not disapprove");
			}	
		}
    }
	
	private JButton getButtonBack()
	{
		if(btnBack==null)
		{
			btnBack=new JButton();
			btnBack.setText("Back");
			btnBack.setBounds(350, 300, 100, 40);
		}
		return btnBack;
	}
	
	private JLabel getLabelHeader()
	{
		if(lblHeader==null)
		{
			lblHeader=new JLabel();
			lblHeader.setText("Memebrs List");
			lblHeader.setBounds(this.getWidth()/2-100, 20, 300, 20);
			lblHeader.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,25));
		}
		return lblHeader;
	}
	
	private JPanel getPanel()
	{
		if(panel==null)
		{
			panel=new JPanel();
			panel.setSize(this.getWidth(),this.getHeight());
			panel.setLayout(null);
						
			// label
			panel.add(getLabelHeader());
			
			// table
			panel.add(getScrollTable());

			// button
			panel.add(getButtonBack());
			
		}
		return panel;
	}
	
	public MemberApprovalGUI()
	{
		super();
		init();
	}
	
	private void init()
	{
		map=new MemberApproval();
		this.setBounds((int)Main.dim.getWidth()/2-260, (int)Main.dim.getHeight()/2-275, 500, 400);
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.setTitle("Zer Lah - Members List");
		this.setResizable(false);
		this.add(getPanel());
		this.setVisible(true);
	}
}
